package leetcode.weekly.week294;

//Solution4Test
public class Solution4 {

	int mod = (int) 1e9 + 7;

	public int totalStrength(int[] strength) {
		int len = strength.length;
		int[] stack = new int[len];
		int p = 0;
		for (int i = 0; i < strength.length; i++) {
			int cur = strength[i];
			while (p > 0 && strength[stack[p - 1]] > cur) {//
				p--;
			}
			stack[p++] = i;
		}
		long ans = 0;

		return (int) ans;
	}

	public void stackTemplate(int k) {
		char[] cs = new char[] {};
		int len = 0;
		int[] stack = new int[len];
		int p = 0;
		for (int i = 0; i < cs.length && k > 0; i++) {
			char cur = cs[i];
			while (p > 0 && cs[stack[p - 1]] > cur && k > 0) {// k>0是业务限制
				p--;
				k--;
			}
			if (p > 0 || cur != '0') {// 业务限制
				stack[p++] = i;
			}
		}
		// 因为业务跳出循环
		while (k > 0 && p > 0) {
			p--;
			k--;
		}
		// 因为长度跳出循环，处理栈内数据
		while (p > 0) {
//			sb.insert(0, cs[stack[--p]]);
		}
	}

	private int totalStrength(int[] strength, int index) {
		if (index == 0) {
			return strength[0] * strength[0] % mod;
		}
		int ans = this.totalStrength(strength, index - 1);
		ans += strength[index] * strength[index] % mod;
		return ans % mod;
	}

}
